<template>
  <el-dialog
    title="新增保养巡检维修工单"
    :visible.sync="dialogVisible"
    width="550px"
    class="popup height50"
    :before-close="handleClose">
    <div class="scrollbar-wrap">
      <el-scrollbar class="scrollbar-x" style="height:100%">
        <el-form :model="formInline" :rules="rules" ref="formInline" label-width="100px">
          <el-form-item label="工单名称:">
            <el-input v-model.number="formInline.username" clearable></el-input>
          </el-form-item>
          <el-form-item label="工单类型:">
            <el-select v-model="value" filterable clearable placeholder="请选择">
              <el-option
                v-for="item in this.$config.selectList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="优先级:">
            <el-radio-group v-model="modeDevice">
              <el-radio :label="1">紧急</el-radio>
              <el-radio :label="2">普通</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="设备模式:">
            <el-radio-group v-model="modeDevice">
              <el-radio :label="1">设备组</el-radio>
              <el-radio :label="2">单个设备</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="设备组:" v-if="modeDevice == 1">
            <el-select v-model="formInline.username" filterable placeholder="请选择">
              <el-option
                v-for="item in this.$config.selectList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="单个设备:" v-if="modeDevice == 2">
            <tree-select ref="treeSelect"
              :data="officeTreeList"
              :el="'formInline.eptIdList'"
              placeholder="请选择"
              multiple
              clearable
              @parent-event="popoverHide"
              @popoverHide="popoverHide">
            </tree-select>
          </el-form-item>
          <el-form-item label="工单模式:">
            <el-radio-group v-model="modePlan">
              <el-radio :label="1">周期</el-radio>
              <el-radio :label="2">自定义</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="工单周期:" v-if="modePlan == 1">
            <el-select v-model="formInline.realname" filterable clearable placeholder="请选择">
              <el-option
                v-for="item in this.$config.cycleList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="开始时间:" v-if="modePlan == 2">
            <el-date-picker
              v-model="formInline.startTime"
              :picker-options="startTime"
              type="datetime"
              placeholder="请选择开始时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="结束时间:" v-if="modePlan == 2">
            <el-date-picker
              v-model="formInline.endTime"
              :picker-options="endTime"
              type="datetime"
              placeholder="请选择结束时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="执行次数:" prop="age">
            <el-input v-model.number="formInline.age" clearable></el-input>
          </el-form-item>
        </el-form>
      </el-scrollbar>
    </div>
    
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" size="small"  @click="handleAdd">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import TreeSelect from '@/components/TreeSelect'
import datetime from '@/mixins/datetime'
import { addPersonnel } from '@/api/personnel'
export default {
  mixins: [ datetime ],
  components: {
    TreeSelect
  },
  data() {
    return {
      dialogVisible: false,
      modePlan: 1,
      modeDevice: 1,
      officeTreeList: this.$config.treeList,
      formInline: {
        username: '',
        password: '',
        phone: '',
        role: ['1'],
        startTime: '',
        endTime: ''
      },

    };
  },
  computed: {
    rules() {
      return {
        // pass: [
        //   { required: true, message: '请输入活动名称', trigger: 'blur' },
        // ],
        // checkPass: [
        //   { required: true, message: '请输入活动名称', trigger: 'blur' },
        // ],
        // age: [
        //   { required: true, message: '请输入活动名称', trigger: 'blur' },
        // ]
      }
    }
      
  },
  methods: {
    togglePopup(flag) {
      this.dialogVisible = flag
    },
    handleClose(done) {
     this.togglePopup(false)
    },
    popoverHide (checkedIds, checkedData, el) {
      console.log(checkedIds, checkedData)
      if (el) {
        eval( ' ( this.' + el +' = checkedIds) ' )
        console.log(2,this.formInline.eptIdList)
      }
    },
    handleAdd() {
      addPersonnel(this.formInline)
        .then(res => {
          if (res.code === 0) {
            this.$message.success(res.msg || '成功')
            this.togglePopup(false)
            this.$emit('refresh')
          }
        })
        .catch(err => {
          this.togglePopup(false)
        })
    }
  }
};
</script>

<style lang="scss" scoped>
@import '~styles/popup.scss'
</style>